/*
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      DocumentEntryContainer.hpp
 *
 * $FTS$
 */
#ifndef _DOCUMENT_ENTRY_CONTAINER_HPP__
#define _DOCUMENT_ENTRY_CONTAINER_HPP__ 1

/**
  @file DocumentEntryContainer.hpp
  @brief Container for document entry
*/

// C Includes
#include <stdlib.h>
#include <string.h>

// Local Includes
#include <FTS.hpp>

namespace FTSS // FullTextSearchSystem
{

/**
  @class DocumentEntryContainer DocumentEntryContainer.hpp <DocumentEntryContainer.hpp>
  @brief Container for document entry
*/
class DocumentEntryContainer
{
private:
	struct _DocumentEntryContainer
	{
		/** Container capacity      */
		UINT_32            capacity;
		/** Container size      */
		UINT_32            size;
		/** Document Entry          */
		DocumentEntry    * document_entry;
		/** Container capacity      */
		INT_32             refcount;
		/** Constructor */
		inline _DocumentEntryContainer(): capacity(0), size(0), document_entry(NULL), refcount(1) { ;; }
	};
	/** Storage */
	_DocumentEntryContainer * pContainer;

	/**
	  @brief Decrease number of references and destroy object if need
	*/
	void Destroy() throw()
	{
		-- pContainer -> refcount;
		if (pContainer -> refcount == 0)
		{
			::operator delete(pContainer -> document_entry);
			delete pContainer;
		}
	}

public:
	/**
	  @brief Constructor
	*/
	inline DocumentEntryContainer()
	{
		pContainer = new _DocumentEntryContainer;

		pContainer -> document_entry = (DocumentEntry *)::operator new(sizeof(DocumentEntry));
		pContainer -> document_entry -> size = sizeof(DocumentEntry) - sizeof(WordEntry);
		pContainer -> capacity = 1;
	}

	/**
	  @brief Copy constructor
	  @param oRhs - object to copy
	*/
	inline DocumentEntryContainer(const DocumentEntryContainer & oRhs) throw(): pContainer(oRhs.pContainer)
	{
		++ pContainer -> refcount;
	}

	/**
	  @brief Operator =
	  @param oRhs - object to copy
	*/
	DocumentEntryContainer & operator=(const DocumentEntryContainer & oRhs) throw()
	{
		if (this == &oRhs) { return *this; }

		Destroy();

		pContainer = oRhs.pContainer;
		++ pContainer -> refcount;
		return *this;
	}

	/**
	  @brief A destructor
	*/
	inline ~DocumentEntryContainer() throw()
	{
		Destroy();
	}

	/**
	  @brief Push element into container
	  @param oWordEntry - element to push
	*/
	inline void push_back(const WordEntry & oWordEntry)
	{
		if (pContainer -> size == pContainer -> capacity)
		{
			DocumentEntry * pTMP = pContainer -> document_entry;
			pContainer -> capacity = pContainer -> capacity << 1;
			pContainer -> document_entry = (DocumentEntry *)::operator new(sizeof(DocumentEntry) * pContainer -> capacity);

			memcpy(pContainer -> document_entry, pTMP, pTMP -> size);
			::operator delete(pTMP);
		}
		pContainer -> document_entry -> words[pContainer -> size] = oWordEntry;
		++pContainer -> size;
	}

	/**
	  @brief Get maximum vector capacity
	  @return Maximum vector capacity
	*/
	inline void StoreDocumentId(const DocumentId_t & iDocId) { pContainer -> document_entry -> doc_id = iDocId; }

	/**
	  @brief Get number of elements
	  @return number of elements
	*/
	inline UINT_32 size()     const { return pContainer -> size; }

	/**
	  @brief Get maximum vector capacity
	  @return Maximum vector capacity
	*/
	inline UINT_32 capacity() const { return pContainer -> capacity; }

	/**
	  @brief Get number of elements
	  @return number of elements
	*/
	inline void erase()
	{
		pContainer -> document_entry -> doc_id = 0;
		pContainer -> size = 0;
	}

	/**
	  @brief Swap two containers
	  @return Maximum vector capacity
	*/
	inline void swap(DocumentEntryContainer & oRhs)
	{
		DocumentEntryContainer oTMP(oRhs);
		oRhs = *this;
		*this = oTMP;
	}

	/**
	  @brief Get stored data
	  @return Document entry
	*/
	inline DocumentEntry * GetData()
	{
		return pContainer -> document_entry;
	}
};

} // namespace FTSS
#endif // _DOCUMENT_ENTRY_CONTAINER_HPP__
// End.
